<template>
  <div class="playActivity">
    <top-bar :title="'游戏活动'"/>
    <!-- 充值礼包 -->
    <div class="scroll">
      <div class="recharge" :class="[rebateList.length == 0 ? 'none' : '']">
        <div class="rechargeBg drawStyleBg" v-if="rebateList.length != 0">
          返利活动
        </div>
        <div class="recharge-content" v-if="newsList">
          <div class="recharge-items" v-for="item in rebateList" :key="item.id"
               @click="$router.push(`/me/activityDetails/${item.news_id}`)">
            <div class="recharge-items-left">
              <div>
                <span>{{ item.title }}</span>
                <span :class="[isDuringDate(dateTransform(item.start_time),dateTransform(item.end_time))==1?'haveInHand':
                  (isDuringDate(dateTransform(item.start_time),dateTransform(item.end_time))==2?'finished':'haveNotStarted')]">
                  {{
                    isDuringDate(dateTransform(item.start_time), dateTransform(item.end_time)) == 1 ? '进行中' :
                      (isDuringDate(dateTransform(item.start_time), dateTransform(item.end_time)) == 2 ? '已结束' : '未开始')
                  }}
                </span>
              </div>
              <span class="overflow">
                <span style="color: #FD7040;">{{ item.rebate_type == 0 ? '手动返利' : '自动发放' }}</span>
                <span class="spansTrue">{{ unescapeHTML(item.content) }}</span>
              </span>
            </div>
            <div class="recharge-items-right"
                 :class="[item.rebate_type==0 ? 'application' : 'check']">
              {{ item.rebate_type == 0 ? ' 申请' : '查看' }}
            </div>
          </div>
        </div>
      </div>
      <!-- 免费礼包 -->
      <div class="free">
        <div class="freeBg drawStyleBg" v-if="noticeList.length != 0">
          活动公告
        </div>
        <div class="recharge-content" v-if="newsList">
          <div class="recharge-items" v-for="item in noticeList" :key="item.id"
               @click="$router.push(`/me/activityDetails/${item.news_id}`)">
            <div class="recharge-items-left">
              <div>
                <span>{{ item.title }}</span>
                <span :class="[isDuringDate(dateTransform(item.start_time),dateTransform(item.end_time))==1?'haveInHand':
                  (isDuringDate(dateTransform(item.start_time),dateTransform(item.end_time))==2?'finished':'haveNotStarted')]">
                  {{
                    isDuringDate(dateTransform(item.start_time), dateTransform(item.end_time)) == 1 ? '进行中' :
                      (isDuringDate(dateTransform(item.start_time), dateTransform(item.end_time)) == 2 ? '已结束' : '未开始')
                  }}
                </span>
              </div>
              <span class="overflow spansTrue" v-html="unescapeHTML(item.content)"></span>
            </div>
            <div class="recharge-items-right check" >查看
            </div>
          </div>
        </div>
      </div>
      <div v-if="newsList.count == 0" class="list-empty">
        <i class="iconfont icon-kong"></i>
        <p>暂时没有活动哦~</p>
      </div>
    </div>
  </div>
</template>

<script>
import topBar from './topBar'

export default {
  name: 'playActivity',
  components: {
    topBar
  },
  data () {
    return {
      rebateList: [],
      noticeList: []
    }
  },
  computed: {
    unescapeHTML (a) {
      return function (a) {
        a = '' + a
        a = a.replace(/&lt;/g, '<')
          .replace(/&gt;/g, '>')
          .replace(/&amp;/g, '&')
          .replace(/&quot;/g, '"')
          .replace(/&apos;/g, '\'')
          .replace(/&apos;/g, '\'')
          .replace(/<[^>]+>/g, '')
        return a
      }
    },
    newsList () {
      this.rebateList = []
      this.noticeList = []
      for (let i of this.$store.state.newsList.list) {
        if (i.rebate == 1) {
          this.rebateList.push(i)
        } else if (i.rebate == 0) {
          this.noticeList.push(i)
        }
      }
      return this.$store.state.newsList
    }
  },
  created () {
    this.$store.dispatch('getNewsList', {
      type: 2
    })
  },
  methods: {
    // 判断活动三个状态进行中、已结束、未开始
    isDuringDate (beginDateStr, endDateStr) {
      let curDate = new Date(),
        beginDate = new Date(beginDateStr),
        endDate = new Date(endDateStr)
      if (curDate >= beginDate && curDate <= endDate) {
        // console.log("进行中");
        return 1
      } else if (curDate > endDate) {
        return 2
      } else if (curDate < beginDate) {
        return 3
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.playActivity {
  padding: 0.75rem 0;

  /deep/ .title {
    padding: 0 0.48rem;
    margin-bottom: 0.48rem;
  }

  .scroll {
    height: 8.5rem;
    overflow: scroll;

    .drawStyleBg {
      width: 2rem;
      height: 0.75rem;
      font-size: 0.37rem;
      font-family: SourceHanSansCN-Medium, SourceHanSansCN;
      font-weight: 500;
      color: #FFFFFF;
      line-height: 0.75rem;
      text-align: center;
      letter-spacing: 0.02rem;
    }

    .recharge, .free {
      .rechargeBg {
        margin-top: 0;
        background: url("../../assets/images/personal_center/recharge.png") no-repeat;
        background-size: 100% 100%;
      }

      .recharge-content {
        margin-top: 0.16rem;
        padding: 0 0.48rem;

        .recharge-items {
          width: 100%;
          height: 2rem;
          background: rgba(255, 255, 255, 0.6);
          border-radius: 0.27rem;
          padding: 0.5rem 0.37rem 0.5rem 0.45rem;
          align-items: center;
          display: flex;
          margin-bottom: 0.32rem;

          .recharge-items-left {
            display: flex;
            flex-direction: column;
            flex: 1;

            > div {
              display: flex;
              align-items: center;

              > span:nth-child(1) {
                font-size: 0.35rem;
                font-family: SourceHanSansCN-Bold, SourceHanSansCN;
                font-weight: bold;
                color: #181818;
              }

              > span:nth-child(2) {
                width: 1.17rem;
                height: 0.45rem;
                border-radius: 0.24rem;
                font-size: 0.27rem;
                font-family: SourceHanSansCN-Medium, SourceHanSansCN;
                font-weight: 500;
                text-align: center;
                line-height: 0.45rem;
                margin-left: 0.16rem;
              }

              .haveInHand {
                background: linear-gradient(90deg, #FF0000 0%, #FFD500 100%);
                color: #FFFFFF;
              }

              .finished {
                background: #F5F5F5;
                color: #B5B5B5;
              }

              .haveNotStarted {
                background: #F5F5F5;
                color: #B5B5B5;
              }
            }

            .overflow {
              /deep/ p {
                width: 6.4rem;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
              }

              margin-top: 0.24rem;
              font-size: 0.27rem;
              font-family: SourceHanSansCN-Regular, SourceHanSansCN;
              font-weight: 400;
              color: #888888;

              .spansTrue {
                width: 80%;
                margin-left: 1.27rem;
                margin-top: -0.27rem;
              }
            }

            .spansTrue {
              width: 90%;
            }
          }

          .recharge-items-right {
            width: 1.49rem;
            height: 0.64rem;
            border-radius: 0.33rem;
            font-size: 0.32rem;
            font-family: SourceHanSansCN-Regular, SourceHanSansCN;
            font-weight: 400;
            text-align: center;
            line-height: 0.64rem;
          }

          .check {
            background: #FFD200;
            color: #000000;
          }

          .application {
            background: #FF0000;
            color: #FFFFFF;
          }
        }
      }
    }

    .recharge {
      margin-bottom: 0.37rem;
    }

    .none {
      margin: 0;
    }

    .freeBg {
      background: url("../../assets/images/personal_center/free.png") no-repeat;
      background-size: 100% 100%;
    }
  }
}
</style>
